<template>
  <div class="table">
    <div style="float: left;margin-left: 40px; width: 100px;margin-bottom: 10px">
    <el-button type="warning" size="mini" @click="dialogFormVisible = true">包间登记</el-button>
  </div>
    <div class="search-box" style="width: 200px;float: right;margin-bottom: 10px">
      <el-input
          class="search"
          size="mini"
          v-model="search"
          placeholder="输入关键字搜索"/>
    </div>
    <el-dialog title=""  :visible.sync="dialogFormVisible" center>
      <div style="margin-left: 140px">
        <el-form ref="user" :model="form" label-width="80px" style="width: 380px;height: 440px">
          <h2 style="color: #ffd04b;padding-left: 166px">顾客信息登记</h2><br>
          <el-form-item label="姓名:" >
            <el-input v-model="form.userName" ></el-input>
          </el-form-item>
          <el-form-item label="电话:" >
            <el-input v-model="form.phone" ></el-input>
          </el-form-item>
          <el-form-item label="包间编号:">
            <el-input v-model.number="form.id" type="text" ></el-input>
          </el-form-item>
          <el-form-item label="使用时长:">
            <el-input v-model.number="form.useTime"></el-input>
          </el-form-item>
          <el-form-item label="包间费用:">
            <el-input v-model.number="form.cost"></el-input>
          </el-form-item>
          <el-form-item label="酒水费用:">
            <el-input v-model.number="form.orderCost"></el-input>
          </el-form-item>
          <el-form-item label="包间区域:">
            <el-select v-model="form.area" placeholder="请选择区域" >
              <el-option label="A区" value="A区"></el-option>
              <el-option label="B区" value="B区"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        </div>
      <div slot="footer" class="dialog-footer" >
        <el-button type="warning" @click="onsubmit()">确 定</el-button>
        <el-button type="" @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-table
        :data="tables"
        height="650px"
        style="width: 100%"
        :header-cell-style="{background:'#ffd04b',color:'#606266'}"
        v-loading="loading"
    >
      <el-table-column
          align="center"
          prop="id"
          label="包间编号"
          width="180">
      </el-table-column>
      <el-table-column
          align="center"
          prop="useTime"
          label="时长"
          width="180">
      </el-table-column>
      <el-table-column
          align="center"
          prop="userName"
          label="姓名">
      </el-table-column>
      <el-table-column
          align="center"
          prop="phone"
          label="电话">
      </el-table-column>
      <el-table-column
          align="center"
          prop="area"
          label="区域">
      </el-table-column>

      <el-table-column
          align="center"
          prop="cost"
          label="费用">
      </el-table-column>
      <el-table-column
          align="center"
          label="操作"
          width="80px">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="reDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name:"registration",
  created() {
    axios.get('http://localhost:8181/registration/list').then(response=>{
      console.log(response.data)
      this.tableData = response.data
      if(response.data){
        setTimeout(() => {
          //代码
          this.loading = false
        }, 500);
      }
    })
  },

    data() {
      return {
        search:'',
        tableData:[

        ],
        dialogFormVisible:false,
        form: {
          userName:'',
          phone:'',
          id: '',
          useTime:'',
          cost:'',
          orderCost:'',
          area: ''
        },
        loading:true
      }
    },

  methods: {
    onsubmit() {
      axios.put('http://120.24.48.65:8282/registration/insert',this.form).then(response => {
        console.log(response.data)
        this.dialogFormVisible = false
        if (response.data){
          //登记成功之后，改变包间占用状态
          axios.put('http://120.24.48.65:8282/rooms/update',{
            id:this.form.id,
            status:'占用',
            statusId:1
          })
          //登记成功之后，向账单添加消费记录
          axios.put('http://120.24.48.65:8282/bill/save',{
            roomId:this.form.id,
            roomBill:this.form.cost+this.form.orderCost
          })
          this.$alert('登记成功！','添加数据',{
            callback:action => {
              //刷新页面
              location.reload()
            }
          });
        }
      });

    },
    reDelete(row) {
      this.$confirm('是否确定要删除' + row.userName + '?', '删除数据', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete('http://120.24.48.65:8282/registration/delete/'+row.id).then(response => {
          if (response.data) {
            //删除订单，让包间状态设置未空
            axios.put('http://120.24.48.65:8282/rooms/update',{
              id:row.id,
              status:'空',
              statusId:0
            })
            this.$alert(row.userName + '删除成功?', '删除数据', {
              callback: action => {
                location.reload()
              }
            });
          }
        })
      }).catch(() => {
      })
    },

  },
  computed:{
    tables:function (){
      let search = this.search
      if(search){
        return this.tableData.filter(function (dataNews){
          return Object.keys(dataNews).some(function (key) {
            return String(dataNews[key]).toLowerCase().indexOf(search) > -1
          })
        })
      }
      return this.tableData
    }
  },
  }
</script>
<style>

</style>